package com.lst.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.lst.api.entity.CompanyInfo;
import com.lst.api.mapper.CompanyInfoMapper;
import com.lst.api.service.ICompanyInfoService;
import com.lst.api.service.ITaxInfoService;
import com.lst.api.vo.CompanyInfoVo;
import com.lst.api.vo.TaxInfoVo;
import com.lst.tools.mybatis.service.impl.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 公司信息业务层实现
 *
 * @author Frank
 * @date 2019-12-30 10:31:37
 * @version: V1.0
 */
@Service
public class CompanyInfoService extends BaseService<CompanyInfoMapper, CompanyInfo> implements ICompanyInfoService {

    @Autowired
    private ITaxInfoService taxInfoService;

    @Override
    public boolean saveOrUpdate(long userId, String applyNo, CompanyInfoVo companyInfoVo, List<TaxInfoVo> taxInfoList) {
        if (companyInfoVo == null || companyInfoVo.isAllFieldNull()) {
            return false;
        }
        companyInfoVo.check();
        CompanyInfo companyInfo = new CompanyInfo(userId, applyNo, companyInfoVo);
        QueryWrapper<CompanyInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .eq(CompanyInfo.APPLY_NO, applyNo)
                .eq(CompanyInfo.PROMO_USER_ID, userId);
        int ret = super.saveNotExist(companyInfo, queryWrapper);
        if (ret == 0) {
            UpdateWrapper<CompanyInfo> updateWrapper = new UpdateWrapper<>();
            updateWrapper
                    .eq(CompanyInfo.APPLY_NO, applyNo)
                    .eq(CompanyInfo.PROMO_USER_ID, userId)
                    .set(CompanyInfo.UPDATE_AT, new Date())
                    .set(CompanyInfo.COMPANY_NAME, companyInfo.getCompanyName())
                    .set(CompanyInfo.COMPANY_TYPE_ID, companyInfo.getCompanyTypeId())
                    .set(CompanyInfo.COMPANY_TYPE_OTHER, companyInfo.getCompanyTypeOther())
                    .set(CompanyInfo.BUSINESS_PLACE_ID, companyInfo.getBusinessPlaceId())
                    .set(CompanyInfo.BUSINESS_PLACE_RENT, companyInfo.getBusinessPlaceRent())
                    .set(CompanyInfo.BUSINESS_LICENSE_STATUS, companyInfo.getBusinessLicenseStatus())
                    .set(CompanyInfo.ESTABLISH_AT, companyInfo.getEstablishAt())
                    .set(CompanyInfo.INDUSTRY_TYPE_ID, companyInfo.getIndustryTypeId())
                    .set(CompanyInfo.MONTH_TURNOVER_AMOUNT, companyInfo.getMonthTurnoverAmount())
                    .set(CompanyInfo.ANNUAL_TAX_AMOUNT, companyInfo.getAnnualTaxAmount())
                    .set(CompanyInfo.ANNUAL_INVOICE_AMOUNT,  companyInfo.getAnnualInvoiceAmount());
            return super.update(updateWrapper);
        }
        taxInfoService.saveOrUpdate(userId, applyNo, taxInfoList);
        return true;
    }

    @Override
    public CompanyInfo getByApplyNo(String applyNo) {
        QueryWrapper<CompanyInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(CompanyInfo.APPLY_NO, applyNo);
        return super.getOne(queryWrapper);
    }
}